package com.idp.demo.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.idp.common.base.BaseController;
import com.idp.common.model.PageModel;
import com.idp.common.model.ResultModel;
import com.idp.demo.dto.GoodsDto;
import com.idp.demo.dto.GoodsSearchDto;
import com.idp.demo.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品Controller
 * @author King
 */
@Api(tags="商品")
@RestController
@RequestMapping("/goods/")
public class GoodsController extends BaseController {

	@Autowired
	private GoodsService goodsService;

	/**
	 * 分页查询
	 * @param goodsSearchDto
	 * @return
	 */
	@ApiOperation(value="分页查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "page", value = "页数", paramType = "query", required = true, defaultValue = "1"),
		@ApiImplicitParam(name = "rows", value = "每页行数", paramType = "query", required = true, defaultValue = "10")
	})
	@PostMapping("findByPage")
	public ResultModel<PageModel<GoodsDto>> findByPage(@RequestBody GoodsSearchDto goodsSearchDto) {
		return ResultModel.success(goodsService.findByPage(goodsSearchDto));
	}

	/**
	 * 通过id查询
	 * @param goodsDto
	 * @return
	 */
	@ApiOperation(value="通过id查询")
	@ApiOperationSupport(includeParameters = {"goodsDto.id"})
	@PostMapping("getById")
	public ResultModel<GoodsDto> getById(@RequestBody GoodsDto goodsDto) {
		return ResultModel.success(goodsService.getById(goodsDto.getId()));
	}

	/**
	 * 新增
	 * @param goodsDto
	 * @return
	 */
	@ApiOperation(value="新增")
	@ApiOperationSupport(ignoreParameters = {"goodsDto.id"})
	@PostMapping("add")
	public ResultModel add(@RequestBody GoodsDto goodsDto) {
		goodsService.add(goodsDto);
		return ResultModel.success("新增成功", null);
	}

	/**
	 * 修改
	 * @param goodsDto
	 * @return
	 */
	@ApiOperation(value="修改")
	@PostMapping("update")
	public ResultModel update(@RequestBody GoodsDto goodsDto) {
		goodsService.update(goodsDto);
		return ResultModel.success("修改成功", null);
	}

	/**
	 * 删除
	 * @param goodsDto
	 * @return
	 */
	@ApiOperation(value="删除")
	@ApiOperationSupport(includeParameters = {"goodsDto.id"})
	@PostMapping("delete")
	public ResultModel delete(@RequestBody GoodsDto goodsDto) {
		goodsService.delete(goodsDto.getId());
		return ResultModel.success("删除成功", null);
	}

}
